package com.liseen.loader
{
	import flash.events.ErrorEvent;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.HTTPStatusEvent;
	import flash.events.IEventDispatcher;
	import flash.events.ProgressEvent;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.utils.getTimer;

	/**
	 * LoadItem 加载队列中的单元加载基类
	 * @author Liseen
	 * 
	 */	
	public class LoadItem extends EventDispatcher
	{
		public static const STATUS_STARTED:String = "started";
		public static const STATUS_STOPPED:String = "stopped";
		public static const STATUS_ERROR:String = "error";
		public static const STATUS_WAIT:String = "wait";
		public static const STATUS_FINISHED:String = "finished";
		
		public var content:Object;
		protected var loadContext:LoaderContext;
		
		private var _url:String;
		private var _type:String;
		private var _status:String;
		private var _preventCache:Boolean;
		private var _isLoaded:Boolean;
		private var _bytesLoaded:Number;
		private var _bytesTotal:Number;
		private var _bytesRemaining:Number;
		private var _percentLoaded:Number;
		private var _numTries:int;
		private var _maxTries:int=3;
		protected var urlRequest:URLRequest;
		
		public function LoadItem(url:String,type:String)
		{
			this._url = url;
			this._type = type;
			this._status = LoadItem.STATUS_WAIT;
			urlRequest = new URLRequest(this._url);
			
			loadContext = new LoaderContext(false, ApplicationDomain.currentDomain);
		}
		/**
		 *获取url 
		 * @return 
		 * 
		 */		
		public function get url():String
		{
			return this._url;
		}
		/**
		 *获取加载对象类型 
		 * @return 
		 * 
		 */		
		public function get type():String
		{
			return this._type;
		}
		/**
		 *获取当前加载状态 
		 * @return 
		 * 
		 */		
		public function get status():String
		{
			return this._status
		}
		/**
		 *已经加载了多少自己诶
		 * @return 
		 * 
		 */		
		public function get bytesLoaded():Number
		{
			return this._bytesLoaded;
		}
		/**
		 *总共加载字节数 
		 * @return 
		 * 
		 */		
		public function get bytesTotal():Number
		{
			return this._bytesTotal;
		}
		
		/**
		 *是否防止缓存 
		 * @param value
		 * 
		 */		
		public function set preventCache(value:Boolean):void
		{
			this._preventCache = value;
		}
		/**
		 *加载 
		 * 
		 */		
		public function load():void
		{
			var newURL:String;
			var timestamp:String;
			if(this._preventCache)
			{
				timestamp = "Cache="+getTimer();
				if(this.url.indexOf("?")==-1)
				{
					newURL = this._url+"?"+timestamp;
				}else{
					newURL = this._url+"&"+timestamp;
				}
			}
		}
		
		/**
		 *停止加载 
		 * 
		 */		
		public function stop():void
		{
			if(this._isLoaded)
			{
				return;
			}
			this._status = LoadItem.STATUS_STOPPED;
		}
		/**
		 *初始化加载 
		 * @param event
		 * 
		 */		
		public function onInitHandler(event:Event):void
		{
			this.dispatchEvent(event);
		}
		/**
		 *开始加载 
		 * @param event
		 * 
		 */		
		public function onStartedHandler(event:Event):void
		{
			this._status = LoadItem.STATUS_STARTED;
			this.dispatchEvent(event);
		}
		/**
		 *安全错误 
		 * @param event
		 * 
		 */		
		public function onSecurityErrorHandler(event:ErrorEvent):void
		{
			this._status = LoadItem.STATUS_ERROR;
			var errorEvent:ErrorEvent = event as ErrorEvent;
			this.dispatchErrorEvent(errorEvent);
			cleanListeners();
		}
		/**
		 *加载ing 
		 * @param event
		 * 
		 */		
		public function onProgressHandler(event:ProgressEvent):void
		{
			_bytesLoaded = event.bytesLoaded;
			_bytesTotal = event.bytesTotal;
			_bytesRemaining = (_bytesTotal - _bytesLoaded);
			_percentLoaded = (_bytesLoaded / _bytesTotal);
			dispatchEvent(event);
		}
		/**
		 *加载完成 
		 * @param event
		 * 
		 */		
		public function onCompleteHandler(event:Event):void{
			this._status = LoadItem.STATUS_FINISHED;
			this._isLoaded = true;
			dispatchEvent(event);
			cleanListeners();
		}
		/**
		 * Http状态
		 * @param event
		 * 
		 */		
		public function onHttpStatusHandler(event:HTTPStatusEvent):void
		{
			
		}
		/**
		 *加载错误 
		 * @param event
		 * 
		 */		
		public function onErrorHandler(event:ErrorEvent):void
		{
			_numTries++;
			if (_numTries < _maxTries){
				trace("尝试重新加载->>"+event);
				this._status = null;
				load();
				event.stopPropagation();
			} else {
				trace("尝试加载失败->>"+event);
				_status = LoadItem.STATUS_ERROR;
				dispatchErrorEvent(event);
				cleanListeners();
			}
		}
		
		/**
		 *清楚数据 
		 * 
		 */		
		public function destroy():void
		{
			this.content = null;
		}	
		
		protected function cleanListeners():void
		{
			
		}
		/**
		 *派发错误信息 
		 * @param event
		 * 
		 */		
		private function dispatchErrorEvent(event:ErrorEvent):void
		{
			this._status = LoadItem.STATUS_ERROR;
			this.dispatchEvent(new ErrorEvent(ErrorEvent.ERROR,false,false,event.text));
		}
	}
}